package service

import (
	"fmt"
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/tools"
	"ksd-social-api/modules/searchkeywords/mapper"
	"ksd-social-api/modules/searchkeywords/model/vo"
	"ksd-social-api/utils/rdb"
)

type SearchKeywordsService struct {
	service.BaseService
}

var searchKeywordsMapper = mapper.SearchKeywordsMapper{}

/**
 * 查找关键词
 * @author feige
 * @date 2024-01-07
 * @version 1.0
 * @desc
 */
func (service *SearchKeywordsService) FindSearhKeywords(systemId uint64) []*vo.SearchKeywordsVo {
	cacheKey := fmt.Sprintf("SYSTEM:MODEL:KEYWORDS:%d", systemId)
	jsonString, _ := rdb.RdbGet(cacheKey)
	searchKeywordsVos := []*vo.SearchKeywordsVo{}
	tools.JsontoStruct(jsonString, &searchKeywordsVos)
	if len(searchKeywordsVos) == 0 {
		searchKeywordsList, _ := searchKeywordsMapper.FindSearhKeywords(systemId)
		for _, searchKeywords := range searchKeywordsList {
			searchKeywordsVo := vo.SearchKeywordsVo{searchKeywords.Keyword}
			searchKeywordsVos = append(searchKeywordsVos, &searchKeywordsVo)
		}
		rdb.RdbSetEX(cacheKey, tools.StructToJson(searchKeywordsVos), 60*60*24)
	}
	return searchKeywordsVos
}
